package boss.portal.config;

import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Converter
public class StringListConverter implements AttributeConverter<List<String>, String> {
    @Override
    public String convertToDatabaseColumn(List<String> strings) {
        return strings != null ?String.join(";",strings):"";
    }

    @Override
    public List<String> convertToEntityAttribute(String s) {
        return s != null? Arrays.asList(s.split(";")):new ArrayList<>();
    }

    public static void main(String[] args) {
        List<String> aa = new ArrayList<String>(){{
            add("1");
            add("2");
            add("3");
        }};
        System.out.println(String.join(";",aa));

        String bb = "1;2;3";
        System.out.println(Arrays.asList(bb.split(";")));
    }
}
